Ext.define('Monkey.view.menu.MenuController', {
    extend: 'Monkey.view.BaseController',
    alias: 'controller.ctrl-menutree',

    init: function () {

    },

    rendererIcon: function (icon) {
        return '<div class="x-tree-icon" style="background-image:url(' + icon + ')"></div>';
    },

    //是否需要选择所有的子菜单
    checkChange: function (node, checked) {
        var me = this;
        if (!node.data.leaf) {
            var childNodes = node.childNodes;
            for (var i = 0; i < childNodes.length; i++) {
                var child = childNodes[i];
                child.set("checked", checked);
                me.checkChange(child, checked);
            }
        }
    },

    rowdblclick: function (node, record) {
        var menuTreeGrid = this.getReferences().menuTreeGrid;
        menuTreeGrid.getStore().each(function (r) {
            r.set("checked", false);
        });
        record.set("checked", true);
        this.modifyMenu(record);
    },

    //显示新增窗口
    addMenu: function () {
        var menuWindow = Ext.create('Monkey.view.menu.MenuWindow');
        menuWindow.setTitle('新增菜单');
        menuWindow.setIcon('resources/icons/16/plugin_add.png');
        menuWindow.getViewModel().data.isAdd = true;
        menuWindow.show();
    },

    //显示修改窗口
    modifyMenu: function (record) {
        if (Ext.isEmpty(record.data)) {
            var menuTreeGrid = this.getReferences().menuTreeGrid;
            var records = menuTreeGrid.getChecked();
            if (records.length == 0) {
                Ext.Msg.show({
                    title: '提示',
                    message: '请先选中需要修改的菜单',
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR
                });
                return;
            }
            record = records[0];
        }

        var menuWindow = Ext.create('Monkey.view.menu.MenuWindow');
        menuWindow.setTitle('修改菜单');
        menuWindow.setIcon('resources/icons/16/plugin_edit.png');

        var viewModel = menuWindow.getViewModel();
        viewModel.data.isAdd = false;
        viewModel.setModelData(record.data);

        setTimeout(function () {
            var treepicker = menuWindow.down('treepicker');
            treepicker.setValue(record.data.parentId);
        }, 500);
        menuWindow.show();

    },

    removeMenu: function () {
        var me = this;
        var menuTreeGrid = this.getReferences().menuTreeGrid;
        var records = menuTreeGrid.getChecked();
        if (records.length == 0) {
            Ext.Msg.show({
                title: '提示',
                message: '请先选中需要删除的菜单',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }
        Ext.Msg.show({
            title: '请确认',
            message: '确认要删除已选中的菜单吗？',
            buttons: Ext.Msg.OKCANCEL,
            fn: function (buttonId) {
                if (buttonId == 'ok') {
                    var ids = Monkey.Utils.arrayModelToString(records, 'id', ',');
                    me.request(Monkey.URL.SERVER_URL + "/m/menu/" + ids + ".json", 'DELETE', null, function (data) {
                        if (data.success) {
                            Ext.Msg.show({
                                title: '提示',
                                message: data.message,
                                buttons: Ext.Msg.OK,
                                icon: Ext.Msg.INFO
                            });
                            me.reloadMenu();
                        } else {
                            Ext.Msg.show({
                                title: '提示',
                                message: data.message,
                                buttons: Ext.Msg.OK,
                                icon: Ext.Msg.ERROR
                            });
                        }
                    });
                }
            },
            icon: Ext.Msg.INFO
        });
    },

    reloadMenu: function () {
        var menuTreeGrid = Ext.getCmp('menutree').getReferences().menuTreeGrid;
        menuTreeGrid.getStore().reload();
    },

    ok: function () {
        var view = this.getView(),
            viewModel = this.getViewModel(),
            method = 'PUT',
            me = this;
        viewModel.data.parentId = view.down('treepicker').getValue();
        console.log(viewModel.data);
        var form = view.getReferences().form;
        if (!form.isValid()) {
            return;
        }
        if (viewModel.data.isAdd) {
            method = 'POST';
        }
        this.request(Monkey.URL.SERVER_URL + '/m/menu.json', method, viewModel.data, function (data) {
            Ext.Msg.show({
                title: '提示',
                message: data.message,
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.INFO
            });
            if (data.success) {
                view.close();
                me.reloadMenu();
            }
        });

    }

});
